%%%====================================================================
%%% File    : pipe_console.erl
%%% Author  :  <vjache>
%%% Description: This is a simpliest pipe implementation. It may be 
%%%				 considered as a simple reference implementation of 
%%%				 a pipe callback module, and could be used for debug 
%%%				 purposes.
%%%
%%% Created : 24.03.2009
%%%====================================================================

-module(pipe_console).

%%
%% Include files
%%
-include("../include/pipe.hrl").
-include("../include/util.hrl").
%%
%% Exported Functions
%%
-export([
		 new/0,
		 open/1,
		 push/2,
		 flush/1,
		 close/1,
		 test/1]).

%%====================================================================
%% API Functions
%%====================================================================

new() ->
	pipe:pipe_from_module(?MODULE, _Param=nil, _NextPipe=terminal).

open({_Param,terminal=_NextPipe}) -> %%PipeState
    {console_pipe,0}.
push({console_pipe,N},Item) ->
    io:format("#~p ~p [~p]~n", [N,Item,node()]),
	{console_pipe,N+1}.
flush({console_pipe,_N}=S) ->
	io:format("Pipe flushed~n"),
	S.
close({console_pipe,_N}) ->
	io:format("Pipe closed~n"),
	ok.

%%====================================================================
%% Test Functions
%%====================================================================

test(L) ->
	S=pipe:open(new()),
	{IterState,PipeState}=pipe:push_each(S, iter:from_list(L)),
	iter:close(IterState),
	pipe:close(PipeState).


%%====================================================================
%% Local Functions
%%====================================================================

